/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telsoft.cms_admin.validation;

import java.io.IOException;
import java.util.Enumeration;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.application.ResourceHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import telsoft.cms_admin.login.LoginBean;
import telsoft.cms_admin.login.RuleBean;

/**
 *
 * @author Bannt
 */
@ManagedBean
public class redirect implements Filter {

    private String oldURL = "";
    public static final String OLD_PATH = "url";
    private FilterConfig config;
    public static boolean info = false;
    public static int count = 0;

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        String pageRequested = req.getRequestURI().toString();
        req.setCharacterEncoding("UTF-8");
        res.setContentType("text/html; charset=UTF-8");

        Object obj = req.getSession().getAttribute(LoginBean.AUTH_KEY);

        String timeoutUrl = req.getContextPath() + "/tamnhat/login.xhtml";

        if (pageRequested.contains("tamnhat")) {
            if (obj == null) {
                if (req.getSession() != null && req.getSession().isNew()) {
                    if (!pageRequested.contains("login") && pageRequested.replace(req.getContextPath() + "/tamnhat/", "").length() > 0) {
                        oldURL = pageRequested;
                    }

                }
                if (!pageRequested.equals(req.getContextPath() + "/tamnhat/login.xhtml")) {
                    if ("partial/ajax".equals(req.getHeader("Faces-Request"))) {
                        res.setContentType("text/xml");
                        res.getWriter().append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
                                .printf("<partial-response><redirect url=\"%s\"></redirect></partial-response>", timeoutUrl);
                    } else {
                        res.sendRedirect(timeoutUrl);
                    }
                } else {
                    req.getSession().setAttribute("oldURL", oldURL.replace(req.getContextPath(), ""));
                    chain.doFilter(req, res);
                    return;
                }
            } else {
                oldURL = "";
                String userId = "";
                try {
                    userId = req.getSession().getAttribute(LoginBean.USER_ID).toString();
                    if (userId == null) {
                        chain.doFilter(req, res);
                        return;
                    }
                } catch (Exception eId) {
                    eId.printStackTrace();
                    chain.doFilter(req, res);
                    return;
                }
                try {
                    if (pageRequested.equals(req.getContextPath() + "/tamnhat/home.xhtml") || pageRequested.equals(req.getContextPath() + "/tamnhat/login.xhtml") || pageRequested.equals(req.getContextPath() + "/tamnhat/changePass.xhtml") || pageRequested.startsWith(req.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) {
                        chain.doFilter(req, res);
                        return;
                    } else {
                        if (RuleBean.checkURLPermission(userId, pageRequested)) {
                            chain.doFilter(req, res);
                            return;
                        } else {
                            res.sendRedirect(timeoutUrl);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    res.sendRedirect(timeoutUrl);
                }
            }
            if (pageRequested.equals(req.getContextPath() + "/tamnhat/login.xhtml") || pageRequested.startsWith(req.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) {
                chain.doFilter(req, res);
                return;
            }
        } else {
            chain.doFilter(req, res);
            return;
        }
    }

    @Override
    public void init(FilterConfig config) throws ServletException {
        this.config = config;
    }

    @Override
    public void destroy() {
        config = null;
    }

    public void setInfo(boolean info) {
        this.info = info;
    }

    public boolean isInfo() {
        return info;
    }

    public String getOldURL() {
        return oldURL;
    }

    public void setOldURL(String oldURL) {
        this.oldURL = oldURL;
    }
}
